#include <json/json.h>
#include <log4cxx/logger.h>

#include "MetadataSvrProtoHelper.h"

using namespace log4cxx;

static LoggerPtr g_logger = Logger::getLogger("MetadataSvrProtoHelper");

string mds_build_get_md_file_info_response(const char * file_content) {
	if (NULL == file_content) {
                LOG4CXX_ERROR(g_logger, "ns_build_get_file_metadata_svr_info_response error, invalid args");
                return "";
        }
        json_object * j = json_tokener_parse(file_content);
        if (is_error(j)) {
                LOG4CXX_ERROR(g_logger, "in ns_build_get_file_metadata_svr_info_response, parse file content error:" << \
                                        file_content);
                return "";
        }
        json_object * j_result = json_object_new_int(0);
        json_object_object_add(j, "RESULT", j_result);
        string response = json_object_to_json_string(j);
        json_object_put(j);
        return response;
}


string mds_build_get_md_file_info_response(MD_FILE_INFO info) {
	json_object * j = json_object_new_object();
        json_object * j_result = json_object_new_int(0);
	json_object * j_client_md_record = json_object_new_object();
	json_object * j_client_crcs = json_object_new_array();
	json_object * j_data_svr_md_records = json_object_new_array();
	for (vector<uint32_t>::const_iterator iter = info.client_md_record.crcs.begin(); \
		info.client_md_record.crcs.end() != iter; iter++) {
		json_object * j_crc = json_object_new_int(*iter);
		json_object_array_add(j_client_crcs, j_crc);
	}
	json_object_object_add(j_client_md_record,  "CLIENT_CRC", j_client_crcs);
	json_object_object_add(j, "CLIENT_MD_RECORD", j_client_md_record);
	for (map<int, DATA_SVR_MD_RECORD>::const_iterator iter = info.data_svr_md_records.begin(); \
		info.data_svr_md_records.end() != iter; iter++) {
		int ds_id = iter->first;
		int64_t file_size = iter->second.file_size;
		vector<uint32_t> crcs = iter->second.crcs;
		json_object * j_data_svr_md_record = json_object_new_object();
		json_object * j_ds_id = json_object_new_int(ds_id);
		json_object * j_file_size = json_object_new_int(file_size);
		json_object * j_crcs = json_object_new_array();
		for(vector<uint32_t>::const_iterator iter1 = crcs.begin(); crcs.end() != iter1; iter1++) {
			json_object * j_crc = json_object_new_int(*iter1);
			json_object_array_add(j_crcs, j_crc);
		}
		json_object_object_add(j_data_svr_md_record, "DATA_SVR_ID", j_ds_id);
		json_object_object_add(j_data_svr_md_record, "FILE_SIZE", j_file_size);
		json_object_object_add(j_data_svr_md_record, "CRC", j_crcs);
		json_object_array_add(j_data_svr_md_records, j_data_svr_md_record);
	}
	json_object_object_add(j, "DATA_SVR_MD_RECORDS", j_data_svr_md_records);
        json_object_object_add(j, "RESULT", j_result);
        string response = json_object_to_json_string(j);
        json_object_put(j);
        return response;
}

string mds_build_alloc_ds_4_new_file_response(int32_t ds_id, int32_t dir_id, int64_t file_id) {
	json_object * j = json_object_new_object();
	json_object * j_result = json_object_new_int(0);
	json_object * j_ds_id = json_object_new_int(ds_id);
	json_object * j_dir_id = json_object_new_int(dir_id);
	json_object * j_file_id = json_object_new_int(file_id);
	json_object_object_add(j, "RESULT", j_result);
	json_object_object_add(j, "DS_ID", j_ds_id);
	json_object_object_add(j, "DIR_ID", j_dir_id);
	json_object_object_add(j, "FILE_ID", j_file_id);
	string response = json_object_to_json_string(j);
	json_object_put(j);
	return response;

}
